﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using GNSCore.Model;
using GNSCore.Model.Context;
using GNSCore.Repositories.IOC;
using LocaleHelper=GNSWebSite.Helpers.LocaleHelper;

namespace GNSWebSite
{
    public class Global : System.Web.HttpApplication
    {
        void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
           LocaleHelper.SetCurrentCulture();
        }

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            IOCManager.ConfigureContainer(new GNSDBEntities());
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            if (RequestIsSentToAspxPage())
            {
                DisposeContext();
            }
        }

        private static bool RequestIsSentToAspxPage()
        {
            return HttpContext.Current.Request.Path.Contains("aspx");
        }

        private static void DisposeContext()
        {
            IGNSContext context = IOCManager.IOCContainer.Resolve<IGNSContext>();

            if (context != null)
            {
                context.Dispose();
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}